<?php


require_once 'JSON.php';

$php_path = dirname(__FILE__) . '/';
$php_url = dirname($_SERVER['PHP_SELF']) . '/';
//
//$save_path = $php_path . '../attached/';
//$save_url = $php_url . '../attached/';


$date = date('m') . '_' . date('Y');
$day = date('d');

//$base = './../../pictures/tintuc/news_images/';

$baseShow = 'http://' . $_SERVER['HTTP_HOST'] . '/tre-today/pictures/tintuc/news_images/' . $date . '/';
//$baseShow = 'http://' . $_SERVER['HTTP_HOST'] . '/pictures/tintuc/news_images/' . $date . '/';
$basePath = './../../pictures/tintuc/news_images/' . $date . '/';


if (!is_dir($basePath)) {
    mkdir($basePath, 0777);
}
if (!is_dir($basePath . '/' . $day)) {
    mkdir($basePath . '/' . $day, 0777);
    mkdir($basePath . '/' . $day . '/thumbs', 0777);
}

$save_path = $basePath . $day . '/';
$save_url = $baseShow . $day . '/';


$ext_arr = array(
    'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp'),
);
//最大文件大小
$max_size = 1000000;

//$save_path = realpath($save_path) . '/';

if (empty($_FILES) === false) {
    $file_name = $_FILES['imgFile']['name'];
    $tmp_name = $_FILES['imgFile']['tmp_name'];
    $file_size = $_FILES['imgFile']['size'];
    if (!$file_name) {
        alert("Please select a file to upload.");
    }
    //check directory to save
    if (@is_dir($save_path) === false) {
        alert("Path NOT exists");
    }
    //check permission
    if (@is_writable($save_path) === false) {
        alert("No permission to write.");
    }
    //check if it has been uploaded
    if (@is_uploaded_file($tmp_name) === false) {
        alert("This file has been uploaded before.");
    }
    //check file size
    if ($file_size > $max_size) {
        alert("File size exceed");
    }

    $temp_arr = explode(".", $file_name);
    $file_ext = array_pop($temp_arr);
    $file_ext = trim($file_ext);
    $file_ext = strtolower($file_ext);

//    if ($dir_name !== '') {
//        $save_path .= $dir_name . "/";
//        $save_url .= $dir_name . "/";
//        if (!file_exists($save_path)) {
//            mkdir($save_path);
//        }
//    }
//    $ymd = date("Ymd");
//    $save_path .= $ymd . "/";
//    $save_url .= $ymd . "/";
    if (!file_exists($save_path)) {
        mkdir($save_path);
    }
    $new_file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $file_ext;
    $file_path = $save_path . $new_file_name;
    if (move_uploaded_file($tmp_name, $file_path) === false) {
        alert("Attempt to upload file failed");
    }
    @chmod($file_path, 0644);
    $file_url = $save_url . $new_file_name;

    header('Content-type: text/html; charset=UTF-8');
    $json = new Services_JSON();
    echo $json->encode(array('error' => 0, 'url' => $file_url));
    exit;
}

function alert($msg) {
    header('Content-type: text/html; charset=UTF-8');
    $json = new Services_JSON();
    echo $json->encode(array('error' => 1, 'message' => $msg));
    exit;
}
